﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.CarService.Maintains;
using TianShiProject.Models.Common;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.CarService.Maintains
{
    public class MaintainRepository : DapperDbContextRepository, IMaintainRepository
    {
        public MaintainRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }
        /// <summary>
        /// 显示维修记录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="time"></param>
        /// <param name="cardnumber"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<MaintainDto>> GetShowMaintain(string name, DateTime? time, string cardnumber, int pageindex, int pagesize)
        {
            PageResponse<MaintainDto> response = new PageResponse<MaintainDto>();
            var sql = $"select a.*,b.License from Maintain a join Car b on a.CarNumber=b.CarNumber where a.IsDelete=0 ";
            
            if (!string.IsNullOrWhiteSpace(name))
            {
                sql += $" and a.MaintainManager like '%" + name + "%'";
            }
            if (time != null)
            {
                sql += $" and a.MaintainTime='{time}'";
            }
            if (!string.IsNullOrWhiteSpace(cardnumber))
            {
                sql += $" and a.CarNumber='{cardnumber}'";
            }
            var list = await this.QueryAsync<MaintainDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $"order by a.Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<MaintainDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        /// <summary>
        /// 删除维修记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteDelMaintain(int id)
        {
            var sql = $"update Maintain set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 批量删除维修记录
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteDelsMaintain(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Maintain set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        /// <summary>
        /// 编辑修改
        /// </summary>
        /// <param name="MaintainDto"></param>
        /// <returns></returns>
        public async Task<int> PutUpdateMaintain(MaintainDto maintainDto)
        {
            var sql = $"update Maintain set Title='{maintainDto.Title}',CarNumber='{maintainDto.CarNumber}',MaintainMoney='{maintainDto.MaintainMoney}',MaintainManager='{maintainDto.MaintainManager}',MaintainDesc='{maintainDto.MaintainDesc}',MaintainTime='{maintainDto.MaintainTime.ToString("yyyy/MM/dd")}',Remark='{maintainDto.Remark}' where Id='{maintainDto.Id}'";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 添加维修记录
        /// </summary>
        /// <param name="MaintainDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddMaintain(MaintainDto maintainDto)
        {
            var sql = $"insert into Maintain values('{maintainDto.MaintainNumber}','{maintainDto.Title}','{maintainDto.CarNumber}','{maintainDto.MaintainMoney}','{maintainDto.MaintainManager}','{maintainDto.MaintainDesc}','{maintainDto.MaintainTime.ToString("yyyy/MM/dd")}','{maintainDto.Remark}',GETDATE(),0)";
            var list = await ExecuteAsync
               (sql, maintainDto);
            return list;
        }
        /// <summary>
        /// 绑定
        /// </summary>
        /// <returns></returns>
        public async Task<PageResponse<CarDtos>> GetBind()
        {
            PageResponse<CarDtos> response = new PageResponse<CarDtos>();
            var sql = $"select * from Car where IsDelete=0  ";
            response.PageData = await this.QueryAsync<CarDtos>(sql, null);
            return response;
        }
    }
}
